<template>
  <div class="mall">
    <div class="mall-left">
      <van-sidebar v-model="activeKey">
        <van-sidebar-item
          v-for="tab in tabs"
          :key="`tab-${tab.id}`"
          :to="{ name: 'VueCategory', params: { id: tab.id }}"
          :title="tab.name"
        />
      </van-sidebar>
    </div>
    <div class="mall-right">
      <!-- VueCategory的占位 -->
      <router-view />
    </div>
  </div>
</template>

<script>
export default {
  name: 'Mall',
  data () {
    return {
      activeKey: 0,
      tabs: []
    }
  },
  created () {
    this.$http.getTabs()
      .then(resp => {
        this.tabs = resp.list
        const { id } = this.$route.params
        if (id) {
          this.tabs.forEach((tab, index) => {
            if (tab.id === parseInt(id)) this.activeKey = index
          })
        } else {
          this.$router.push(`/mall/${this.tabs[0].id}`)
        }
      })
  },
  beforeRouteUpdate (to, from, next) {
    // console.log({ to })
    if (to.name !== 'Mall') {
      next()
    }
  }
}
</script>
<style lang="scss" scoped>
.mall {
  display: flex;
  height: 100%;
  background-color: #eeeeee;
  &-left {
    overflow-x: hidden;
    .van-sidebar-item--select::before {
      background-color: #1989fa;
    }
  }
  &-right {
    flex: 1;
    overflow-x: hidden;
  }
}
</style>
